package com.zhengdongzulin.zuke.app.main.apartment


import android.content.Intent
import android.widget.TextView
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.androidktx.core.*
import com.lxj.androidktx.core.color
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.City
import com.zhengdongzulin.zuke.app.data.HomeItem
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkFragmentApartmentBinding
import com.zhengdongzulin.zuke.ext.*

/**
 * jeff
 * 公寓
 * 2021-05-18 11:01:51
 */
class ZKApartmentFragment : BaseFragment<ApartmentVm, ZkFragmentApartmentBinding>() {

    companion object {
        fun newInstance() = ZKApartmentFragment()
    }

    var mselType = "2"

    override fun getLayoutResId(): Int = R.layout.zk_fragment_apartment

    override fun initVM(): ApartmentVm = getVM(ApartmentVm::class.java)

    override fun initView() {
        mView.sel2.setDrawRight(R.mipmap.up_triangle_sel_down)
        mView.sel2.setColor(R.color.color_1273f7)
        mView.contentView.setEnableRefresh(false)
    }

    override fun initData() {}
    override fun lazyLoad() {
        mView.statusView.showLoading()
//        sel1.setOnClickListener { sel1.rotationY = 180f }
        var city = sp().getString("current_city", "")
        if (mShareViewModel.city.value != null) {
            mViewModel.get_house_item_list(mShareViewModel.city.value?.cityId, mselType)
            mView.location.setText(mShareViewModel.city.value?.cityName)
        } else if (!city.isNullOrEmpty()) {
            var mCity = city.toBean<City>()
            mViewModel.get_house_item_list(mCity.cityId, mselType)
            mView.location.setText(mCity.cityName)
        } else {
            mViewModel.get_city_com_list()
        }
    }

    override fun initListener() {
        mView.sel1.setOnClickListener {
            mView.sel1.setDrawRight(R.mipmap.up_triangle_sel_down)
            mView.sel1.setColor(R.color.color_1273f7)
            mView.sel2.setDrawRight(R.mipmap.up_triangle_down)
            mView.sel2.setColor(R.color.black_66)
            mselType = "1"
            lazyLoad()
        }
        mView.sel2.setOnClickListener {
            mView.sel1.setDrawRight(R.mipmap.up_triangle_down)
            mView.sel1.setColor(R.color.black_66)
            mView.sel2.setDrawRight(R.mipmap.up_triangle_sel_down)
            mView.sel2.setColor(R.color.color_1273f7)
            mselType = "2"
            lazyLoad()
        }
        mView.map.setOnClickListener {
            toast("敬请期待")
        }
    }

    override fun onResume() {
        super.onResume()
        if (mShareViewModel.apartmentTypeIsChangeForApartment.value != null && mShareViewModel.apartmentTypeIsChangeForApartment.value!!) {
            lazyLoad()
            mShareViewModel.apartmentTypeIsChangeForApartment.value = false
        }
    }

    override fun startObserve() {
        mViewModel.get_city_com_list.observe(this, Observer {
            mViewModel.get_house_item_list(it[0].cityId, mselType)
            mView.location.setText(it[0].cityName)
        })
        mViewModel.get_house_item_list.observe(this, Observer {
            if (it.size == 0) {
                mView.statusView.showEmpty()
                return@Observer
            }
            val adapter = ListAdpater(it)
            adapter.setOnItemClickListener { adapter, view, position ->
                Intent().put("id", it[position].id)
                    .start(requireActivity(), ZKStoreDetailsActivity::class.java)
            }
            mView.recyclerView.vertical().adapter = adapter
            mView.statusView.showContent()
        })
        mViewModel.get_house_item_list.state.observe(
            this,
            getObserver(mViewModel.get_house_item_list)
        )
    }

    inner class ListAdpater(
        list: MutableList<HomeItem>
    ) : BaseQuickAdapter<HomeItem, BaseViewHolder>(
        R.layout.item_home_recommend,
        list
    ) {
        override fun convert(holder: BaseViewHolder, item: HomeItem) {
            holder.loadImage(R.id.iv_1, item.firstItemPicUrl, R.mipmap.error_empty_zheng, 5f)
            holder.loadImage(R.id.iv_2, item.secondItemPicUrl, R.mipmap.error_empty_zheng, 5f)
            holder.loadImage(R.id.iv_3, item.thirdItemPicUrl, R.mipmap.error_empty_zheng, 5f)
            holder.setText(R.id.name, item.houseItemName)
            holder.setText(R.id.location, item.houseItemAddress)
            var money = holder.getView<TextView>(R.id.money)
            if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                money.text = ""
                money.appendStyleSpan("面议")
            } else {
                money.text = "￥${item.minMoney}"
                money.appendSizeSpan("/月起", 0.46f)
                    .colorSpan(
                        "",
                        money.text.length - 3..money.text.length,
                        context.color(R.color.black_66)
                    )
                    .styleSpan("", 0..money.text.length - 3)
            }
        }
    }
}